/*
 * @Author: Bennty
 * @Date: 2025-01-07 16:02:33
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-01-07 16:03:18
 * @Description: 请填写简介
 */
function digitToChinese (number: any,unit = "元", append = "整"):string {
    // 汉字的数字
    const cnNums = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
    // 基本单位
    const cnIntRadice = ["", "拾", "佰", "仟"];
    // 对应整数部分扩展单位
    const cnIntUnits = ["", "万", "亿", "兆"];
    // 对应小数部分单位
    const cnDecUnits = ["角", "分", "毫", "厘"];
    // 最大处理的数字
    const maxNum = 9999999999999999.99;
    if(number> maxNum) return ' ';
    let chineseStr = '';
    let integerNum = Math.floor(number);
    let decimalNum  = number % 1;
    if(integerNum > 0){
        let zeroCount = 0;
        let integerNumString = String(integerNum);
        let IntLen = integerNumString.length;
        for (let i = 0; i < IntLen; i++) {
            let n = integerNumString[i];
            let p = IntLen - i - 1;
            let q = p / 4;
            let m = p % 4;
            if (n === "0") {
                zeroCount++;
            } else {
                if (zeroCount > 0) {
                    chineseStr += cnNums[0];
                }
                // 归零
                zeroCount = 0;
                chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
            }
            if (m === 0 && zeroCount < 4) {
                chineseStr += cnIntUnits[q];
            }
        }
        chineseStr += unit;
    };
    if (decimalNum != 0) {
        let decimalNumStr = String(decimalNum);
        let decLen = decimalNumStr.length;
        for (let i = 0; i < decLen; i++) {
            let n = decimalNumStr[i];
            if (n !== "0") {
                chineseStr += cnNums[Number(n)] + cnDecUnits[i];
            }
        }
    }
    if (chineseStr === "") {
        chineseStr += cnNums[0] + unit + append;
    } else if (decimalNum == 0) {
        chineseStr += append;
    }
    return chineseStr;
};

export default digitToChinese;